<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/15
 * Time: 10:38
 */

namespace App\Services;
use EasySwoole\EasySwoole\Config;
use think\helper\hash\Md5;

/**
 * Class HelpService
 * @package App\Services
 */
class HelpService
{
    /**文件大小自动转换
     * @param $num
     * @return string
     */
   public static function getFilesize($num) {
        $p = 0;
        $format = 'bytes';
        if( $num > 0 && $num < 1024 ) {
            $p = 0;
            return number_format($num) . ' ' . $format;
        }
        if( $num >= 1024 && $num < pow(1024, 2) ){
            $p = 1;
            $format = 'KB';
        }
        if ( $num >= pow(1024, 2) && $num < pow(1024, 3) ) {
            $p = 2;
            $format = 'MB';
        }
        if ( $num >= pow(1024, 3) && $num < pow(1024, 4) ) {
            $p = 3;
            $format = 'GB';
        }
        if ( $num >= pow(1024, 4) && $num < pow(1024, 5) ) {
            $p = 3;
            $format = 'TB';
        }
        $num /= pow(1024, $p);
        return number_format($num, 3) . ' ' . $format;
    }

    /**创建版本号
     * @param $len
     */
    public static function createVersion(){
        return mt_rand(100000,999999);
    }

    /*
     * 创建全局消息id
     */
    public static function createGlobalMsgId($userId){
        return md5(Config::getInstance()->getConf('APP.CHAT.CURRENT.host').date("YmdHis") . $userId . mt_rand(1000, 9999));
    }


    /**毫秒级时间戳
     * @return float
     */
    public static  function msectime() {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }

    /**时间转毫秒
     * @param $time
     * @return string
     */
    public  static function strDateToMsectime($time)
    {
       list($usec,$sec) = explode(".", $time);
        $date = strtotime($usec);
        $return_data = str_pad($date.$sec, 13, "0", STR_PAD_RIGHT);
        return $return_data;
        #return strtotime($time)*1000;
    }

    /**毫秒时间转字符串日期
     * @param $time
     */
    public static function msectimeToStr($time){
        $time = $time/1000;
        if(strstr($time,'.')){
            sprintf("%01.3f",$time); //小数点。不足三位补0
            list($usec, $sec) = explode(".",$time);
            $sec = str_pad($sec,3,"0",STR_PAD_RIGHT); //不足3位。右边补0
        }else{
            $usec = $time;
            $sec = "000";
        }
        $date = date("Y-m-d H:i:s.x",$usec);
        return str_replace('x', $sec, $date);
    }

    /**创建联合id
     * @param $userId
     * @return int
     */
    public static function createUnionId($userId)
    {
        $len = 9;//亿级别用户
        $baseStr = substr($userId, 0, $len);
        $baseStr = str_pad($baseStr, $len, 0, STR_PAD_RIGHT);
        return date("Ymd") . $baseStr . mt_rand(1000, 9999);
    }

    /**
     * 验证手机号
     *
     * @param string $mobile
     * @return boolean
     */
    public static function isMobile($mobile)
    {
        return (bool)(preg_match('/^(\+86)?(1[3-9][0-9]{9})$/', $mobile));
    }

    /**
     * aes加密
     * @param $str
     * @return string
     */
    public static function aesEncrypt($str,$ak='',$sk='')
    {
        if(empty($ak)){
            $ak = Config::getInstance()->getConf('APP.AES.AES_KEY');
        }
        if(empty($sk)){
            $sk = Config::getInstance()->getConf('APP.AES.IV');
        }
        return base64_encode(openssl_encrypt($str, 'AES-128-CBC', $ak, 1, $sk));
    }

    /**
     * aes解密
     * @param $str
     * @return string
     */
    public static function aesDecrypt($str,$ak='',$sk='')
    {
        if(empty($ak)){
            $ak = Config::getInstance()->getConf('APP.AES.AES_KEY');
        }
        if(empty($sk)){
            $sk = Config::getInstance()->getConf('APP.AES.IV');
        }
        return openssl_decrypt(base64_decode($str), 'AES-128-CBC', $ak, 1, $sk);
    }

    /**创建随机字符串
     * @param $userId
     * @return int
     */
    public static function createNonce($len){
        $strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
        return substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),$len);
    }

    /**自动生成imid
     * @return string
     */
    public static function createImid(){
        $sub = self::createNonce(6);
        return md5(time().$sub.mt_rand(1000,9999));
    }

    /**获取唯一id
     * @param $prefix
     * @param bool $more_entropy
     * @return string
     */
    public static function getUniqueId($prefix){
        return  md5($prefix.uniqid(mt_rand(10000,99999), true));
    }
}